package com.ruoyi.web.controller.epidemic;


import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.epidemic.domain.WxCpSysDept;
import com.ruoyi.epidemic.handler.ContactsSyncHandler;
import com.ruoyi.epidemic.service.IWxCpSysDeptService;
import com.ruoyi.epidemic.service.QRCodeService;
import com.ruoyi.epidemic.service.RequiredWxOauth2;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.impl.WxCpOAuth2ServiceImpl;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.WxCpOauth2UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/epidemic/epidemicMain")
@Slf4j
public class EpidemicMainController {

    private String prefix = "epidemic/epidemicMain";
    @Value("${wechat.cp.corpId}")
    private String corpId;

    @Value("${wechat.cp.appConfigs.agentId}")
    private Integer agentId;

    @Value("${wechat.cp.appConfigs.secret}")
    private String secret;

    @Value("${wechat.cp.appConfigs.token}")
    private String token;

    @Value("${wechat.cp.appConfigs.aesKey}")
    private String aesKey;

    @Autowired
    private ContactsSyncHandler syncHandler;

    @Autowired
    private IWxCpSysDeptService wxCpSysDeptService;

    @Autowired
    private QRCodeService qrCodeService;


    @RequiredWxOauth2
    @GetMapping("")
    public String auth(HttpServletRequest request, HttpServletResponse response) {
        return "/epidemicMain";
    }

    @GetMapping("/callback")
    public String allback(HttpServletRequest request) throws WxErrorException {
        String code = request.getParameter("code");
        return "callback";
    }

    @PostMapping("/contactsSync")
    @ResponseBody
    public String contactsSync(SysUser user) throws WxErrorException {

        syncHandler.sync();

        return "success";
    }

    /**
     * 加载部门列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = wxCpSysDeptService.selectDeptTree(new WxCpSysDept());
        return ztrees;
    }



    @GetMapping("/qrCode")
    public void getQRCode(String codeContent, HttpServletResponse response) {
        try {
            qrCodeService.createQRCode2Stream(codeContent, response);
            log.info("成功生成二维码！");
        } catch (Exception e) {
            log.error("发生错误， 错误信息是：{}！", e.getMessage());
        }
    }


}
